<template>
    <div
        class="detailOfReasonCall"
        v-loading="pageLoading"
        element-loading-background="rgba(255, 255, 255, 0.7)"
    >
        <!-- 来电原因明细报表 -->
        <!-- <PageTitle pageTitle="来电原因明细报表" /> -->
        <div class="formContainer">
            <!-- 搜索表单模块 -->
            <el-form
                class="searchForm searchForm_inline"
                ref="searchForm"
                :model="searchForm"
                :inline="true"
                label-position="right"
                size="small"
                label-width="110px"
                :rules="rules"
            >
                <el-row>
                    <el-form-item label="座席工号:" prop="loginId">
                        <el-input placeholder="请输入座席工号" v-model="searchForm.loginId"></el-input>
                    </el-form-item>
                    <el-form-item label="来电号码:" prop="anis">
                        <el-input placeholder="请输入来电号码" v-model="searchForm.anis"></el-input>
                    </el-form-item>
                    <el-form-item prop="callDtStart" label="开始日期:">
                        <el-date-picker
                            v-model="searchForm.callDtStart"
                            type="datetime"
                            placeholder="请选择开始日期"
                            format="yyyy-MM-dd HH:mm:ss"
                            value-format="yyyy-MM-dd HH:mm:ss"
                            :picker-options="startOptions"
                            @change="startTimeChange"
                        ></el-date-picker>
                    </el-form-item>
                    <el-form-item prop="callDtEnd" label="结束日期:">
                        <el-date-picker
                            v-model="searchForm.callDtEnd"
                            type="datetime"
                            placeholder="请选择结束日期"
                            format="yyyy-MM-dd HH:mm:ss"
                            value-format="yyyy-MM-dd HH:mm:ss"
                            :picker-options="endtOptions"
                            @change="endTimeChange"
                        ></el-date-picker>
                    </el-form-item>
                    <el-form-item label="来电原因大类:" prop="optflag">
                        <el-select
                            placeholder="请选择"
                            v-model="searchForm.reasonType"
                            clearable
                            @change="bigTypeChange"
                        >
                            <el-option
                                v-for="item in bigType"
                                :key="item.operationId"
                                :label="item.operationName"
                                :value="item.operationId"
                            ></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="来电原因小类:" prop="reason">
                        <el-select placeholder="请选择" v-model="searchForm.reason" clearable>
                            <el-option
                                v-for="item in smallType"
                                :key="item.operationId"
                                :label="item.operationName"
                                :value="item.operationId"
                            ></el-option>
                        </el-select>
                    </el-form-item>
                    <el-button
                        type="primary"
                        size="small"
                        icon="el-icon-search"
                        @click="handleSearch"
                    >查 询</el-button>
                    <el-button
                        type="primary"
                        size="small"
                        icon="el-icon-refresh-right"
                        @click="handleReset"
                    >重 置</el-button>
                    <el-button type="primary" size="small" @click="handleExport">导 出</el-button>
                </el-row>
            </el-form>
            <!-- 表格数据展示模块 -->
            <el-table :data="tableData" stripe border class="heard-bg">
                <!-- <el-table-column
                    v-for="(item, index) in tableColumn"
                    :key="index"
                    :label="item.label"
                    :min-width="item.minWidth"
                >
                    <template slot-scope="scope">
                        <span>{{ scope.row[item.value] }}</span>
                    </template>
                </el-table-column>-->

                <el-table-column label="来电原因明细报表">
                    <el-table-column
                        :label="`表格时间：${tableHeader.startTime}-${tableHeader.endTime}`"
                    >
                        <el-table-column label="填写人" width="150px">
                            <template slot-scope="scope">{{scope.row.loginId}}</template>
                        </el-table-column>
                        <el-table-column label="来电号码" width="150px">
                            <template slot-scope="scope">{{scope.row.anis}}</template>
                        </el-table-column>
                        <el-table-column label="卡号" width="200px">
                            <template slot-scope="scope">{{scope.row.cardNo}}</template>
                        </el-table-column>
                        <el-table-column label="填写日期时间" width="200px">
                            <template slot-scope="scope">{{scope.row.callDt}}</template>
                        </el-table-column>
                    </el-table-column>
                    <el-table-column :label="`制表日期：${tableHeader.makeTime}`">
                        <el-table-column label="来电原因大类">
                            <template slot-scope="scope">{{scope.row.reasonTypeValue}}</template>
                        </el-table-column>
                        <el-table-column label="来电原因小类">
                            <template slot-scope="scope">{{scope.row.reasonValue}}</template>
                        </el-table-column>
                    </el-table-column>
                </el-table-column>
            </el-table>

            <Pagination
                :hanleChangeCurrentPage="hanleChangeCurrentPage"
                :handleChangeSize="handleChangeSize"
                :total="total"
                :pageSize="pageSize"
                :currentPage="currentPage"
            />
        </div>
    </div>
</template>

<script>
import moment from 'moment';
import Pagination from '@/components/Pagination/Pagination.vue';
import { getDictSelectList, isNull } from '@/utils/Common';
import request from '@/apis/request';
import { eachProp } from '@/utils/Object';
import { getToken, getTokenHsbankNo } from '@/utils/auth';
import PageTitle from '@/components/pageTitle/PageTitle.vue';

export default {
    name: 'detailOfReasonCall',
    components: {
        Pagination,
        PageTitle,
    },
    data() {
        return {
            currentPage: 1, // 当前分页
            pageSize: 10, // 每页数量
            total: 0, // 数据总数
            pageLoading: false, // 查询按钮loading
            searchForm: { // 搜索表单字段
                callDtStart: '', // 开始日期
                callDtEnd: '', // 截止日期
                reasonType: '', // 大类来电原因
                loginId: '', // 坐席id
                anis: '', // 来电号码
                reason: '', // 小类来电原因
            },
            smallType: [], // 小类选择列表
            bigType: [], // 大类选择下拉
            // 表格展示项
            tableColumn: [
                {
                    label: '填写人',
                    value: 'loginId',
                },
                {
                    label: '来电号码',
                    value: 'anis',
                },
                {
                    label: '卡号',
                    value: 'cardNo',
                },
                {
                    label: '填写日期时间',
                    value: 'callDt',
                },
                {
                    label: '来电原因大类',
                    value: 'reasonTypeValue',
                },
                {
                    label: '来电原因小类',
                    value: 'reasonValue',
                },
            ],
            // 表格数据
            tableData: [{}],
            rules: {
                callDtStart: [
                    { required: true, message: '请选择起始日期', trigger: 'blur' },
                ],
                callDtEnd: [
                    { required: true, message: '请选择截止日期', trigger: 'blur' },
                ],
            },
            // 开始日期选择 限制
            startOptions: {
                disabledDate: (time) => {
                    if (this.searchForm.callDtEnd) {
                        return time.getTime() > new Date(moment(this.searchForm.callDtEnd).format('YYYY-MM-DD')).getTime();
                    }
                },
            },

            // 结束日期选择限制
            endtOptions: {
                disabledDate: (time) => {
                    if (this.searchForm.callDtStart) {
                        return time.getTime() < new Date(moment(this.searchForm.callDtStart).format('YYYY-MM-DD')).getTime();
                    }
                },
            },

            tableHeader: { // 表头显示
                startTime: '', //moment(new Date(new Date().getTime() - (1000 * 60 * 60 * 24 * 30))).format('YYYY-MM-DD HH:mm:ss'), // 开始日期 默认为当前日期往前一个月
                endTime: '', //moment(new Date()).format('YYYY年MM月DD日hh时mm分'), // 截止时间 默认为当前日期
                makeTime: '', //moment(new Date()).format('YYYY年MM月DD日'), //制表时间
            },
        };
    },
    created() { },
    mounted() {
        this.searchForm.callDtStart = moment(new Date(new Date().getTime() - (1000 * 60 * 60 * 24 * 30))).format('YYYY-MM-DD HH:mm:ss');
        this.searchForm.callDtEnd = moment(new Date()).format('YYYY-MM-DD HH:mm:ss');
        this.tableHeader = { // 表头显示
            startTime: moment(new Date(new Date().getTime() - (1000 * 60 * 60 * 24 * 30))).format('YYYY年MM月DD日HH时mm分'), // 开始日期 默认为当前日期往前一个月
            endTime: moment(new Date()).format('YYYY年MM月DD日HH时mm分'), // 截止时间 默认为当前日期
            makeTime: moment(new Date()).format('YYYY年MM月DD日'), //制表时间
        };
        // 初始化方法
        this.initData();
    },

    filters: {


    },
    methods: {
        // 初始化下拉数据
        initData() {
            this.$post.callSummary.mainType().then((res) => {
                this.pageLoading = false;
                if (res.status == 200 && res.data.success) {
                    // console.log(res.data.content,'大类原因')
                    this.bigType = res.data.content || [];
                } else {
                    this.$alert('查询失败', '提示', {
                        customClass: 'error', type: 'error',
                    });
                }
            });
            // 初始化表格数据
            this.getListData();
        },

        startTimeChange() {
            this.tableHeader.startTime = moment(this.searchForm.callDtStart).format('YYYY年MM月DD日HH时mm分');
        },

        endTimeChange() {
            this.tableHeader.endTime = moment(this.searchForm.callDtEnd).format('YYYY年MM月DD日HH时mm分');
        },

        bigTypeChange(row) {
            this.searchForm.reason = '';
            this.$post.callSummary.specificType({ mainType: this.searchForm.reasonType }).then((res) => {
                this.pageLoading = false;
                if (res.status == 200 && res.data.success) {
                    // console.log(res.data.content,'小类原因')
                    this.smallType = res.data.content || [];
                } else {
                    this.$alert('查询失败', '提示', {
                        customClass: 'error', type: 'error',
                    });
                }
            });
        },

        // 获取表格数据
        getListData() {
            this.$refs.searchForm.validate((valid) => {
                if (valid) {
                    this.pageLoading = true;
                    const params = {
                        ...this.searchForm,
                        bankNum: getTokenHsbankNo('HsbankNo'), // 银行号
                        pageNum: this.currentPage,
                        pageSize: this.pageSize,
                        isPage: '',
                    };

                    // 查询接口
                    this.$post.callSummary.callReasonDetail(params).then((res) => {
                        this.pageLoading = false;
                        console.log(res.data, '查询');
                        if (res.data.success) {
                            if (res.data.content != null && res.data.content != '') {
                                this.tableData = res.data.content.records || [];
                                this.total = res.data.content.total;
                            } else {
                                // this.total = 0;
                                // this.$alert(res.data.msg, '提示', {
                                //     customClass: 'warning', type: 'warning',
                                // });
                            }
                        } else {
                            this.$alert('查询失败', '提示', {
                                customClass: 'error', type: 'error',
                            });
                        }
                    });
                } else {

                }
            });
        },

        // 点击导出 -> 导出流水记录表格
        handleExport() {
            this.$refs.searchForm.validate((valid) => {
                if (!valid) return;
                const data = {
                    ...this.searchForm,
                    bankNum: getTokenHsbankNo('HsbankNo'), // 银行号
                    isPage: '',
                };
                // console.log(data)
                let url = '/iccsrv/call/reason/downloadReason?';
                eachProp(data, (prop) => {
                    if (!isNull(data[prop])) {
                        url += `${prop}=${data[prop].toString()}&`;
                    }
                });
                url = url.substring(0, url.length - 1);
                request({
                    url,
                    method: 'get',
                    responseType: 'blob',
                }).then((res) => {
                    // console.log(res, '相应报文')
                    const blobUrl = window.URL.createObjectURL(new Blob([res.data]));
                    const a = document.createElement('a');
                    a.style.display = 'none';
                    a.download = `来电原因明细报表-${moment(new Date()).format('YYYYMMDDHHmmss')}.xls`;
                    a.href = blobUrl;
                    document.body.appendChild(a);
                    a.click();
                    document.body.removeChild(a);
                });
            });
        },

        // 点击查询
        handleSearch() {
            this.tableData = [];
            this.currentPage = 1;
            this.getListData();
        },

        // 点击重置搜索表单
        handleReset() {
            this.$refs.searchForm.resetFields();
            this.tableData = [];
            this.currentPage = 1;
            // this.getListData()
        },

        // 切换分页大小
        handleChangeSize(val) {
            this.pageSize = val;
            // 重置页码
            this.currentPage = 1;
            this.getListData();
        },

        // 切换页码
        hanleChangeCurrentPage(val) {
            this.currentPage = val;
            this.getListData();
        },
    },
};
</script>
<style lang="scss">
.detailOfReasonCall {
    .searchForm {
        .el-select,
        .el-date-editor {
            width: 100%;
            /* max-width: 200px; */
        }
        .el-row {
            text-align: left;
        }
    }
    .tableItem {
        text-align: left !important;
    }
    .formContainer {
        .el-row {
            margin-top: 5px;
        }
    }
}
</style>
<style lang="scss" scoped>
.detailOfReasonCall {
    padding: 20px;
    height: 100%;
    background-color: #fff;
    .formContainer {
        margin-bottom: 20px;
    }
    .formContainer .el-input__inner {
        width: 100%;
        /* max-width: 200px; */
    }
}
</style>
